package com.zh.tea.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.dao.TypeMapper;
import com.zh.tea.entity.Type;
import com.zh.tea.entity.TypeExample;
import com.zh.tea.service.TypeService;
import com.zh.tea.utils.SessionFactoryUtils;

public class TypeServiceImpl implements TypeService {

	@Override
	public int addType(Type type) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TypeMapper tm=session.getMapper(TypeMapper.class);
		int a = tm.insert(type);
		session.commit();
		session.close();
		return a;
	}

	@Override
	public int delType(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TypeMapper tm=session.getMapper(TypeMapper.class);
		int a = tm.deleteByPrimaryKey(id);
		session.commit();
		session.close();
		return a;
	}

	@Override
	public int updateType(Type type) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TypeMapper tm=session.getMapper(TypeMapper.class);
		int a = tm.updateByPrimaryKey(type);
		session.commit();
		session.close();
		return a;
	}

	@Override
	public PageInfo<Type> getTypeByPage(Type type, Integer pagNo, Integer PageSize) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TypeMapper tm=session.getMapper(TypeMapper.class);
		TypeExample uename=new TypeExample();
		TypeExample.Criteria criterianame=uename.createCriteria();
		if(type.getId()!=null){
			criterianame.andIdEqualTo(type.getId());
		}
		if(type.getName()!=null){
			criterianame.andNameEqualTo(type.getName());
		}
		if(type.getPid()!=null){
			criterianame.andPidEqualTo(type.getPid());
		}	
		PageHelper.startPage(pagNo, PageSize);
		List<Type> username=tm.selectByExample(uename);
		PageInfo<Type> pagetea=new PageInfo<>(username);
		return pagetea;
	}

	@Override
	public List<Type> selectAllType() {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TypeMapper tm=session.getMapper(TypeMapper.class);
		TypeExample example=new TypeExample();
		List<Type> list=tm.selectByExample(example);
		return list;
	}

	@Override
	public Type getTypeById(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TypeMapper tm=session.getMapper(TypeMapper.class);
		Type type=tm.selectByPrimaryKey(id);
		
		
		return type;
	}

}
